package com.code.module.permission.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.apache.struts2.ServletActionContext;

import com.code.base.sys.pojo.SysMenu;
import com.code.base.sys.pojo.SysRole;
import com.code.base.sys.pojo.SysUser;
import com.code.base.sys.service.SysMenuService;
import com.code.base.sys.service.SysRoleService;
import com.code.base.sys.service.SysUserService;
import com.code.module.permission.service.PermissionService;
import com.code.util.TreeUtil;

public class PermissionAction {

	@Resource
	private SysMenuService sysMenuService;
	
	@Resource
	private SysUserService sysUserService;
	
	@Resource
	private SysRoleService sysRoleService;
	
	@Resource
	private PermissionService permissionService;
	
	private String sysUserJson;
	
	private SysUser sysUser = new SysUser();
	
	public SysUser getSysUser() {
		return this.sysUser;
	}

	public SysUser setSysUser(SysUser sysUser) {
		return this.sysUser = sysUser;
	}
	
	public String getSysUserJson() {
		return sysUserJson;
	}

	public void setSysUserJson(String sysUserJson) {
		this.sysUserJson = sysUserJson;
		JSONObject jsonObject = JSONObject.fromObject(sysUserJson);
		JSONObject temp = new JSONObject();
		for (Object key : jsonObject.keySet()) {
			if (null != jsonObject.get(key) && !"".equals(jsonObject.get(key))) {
				temp.put(key, jsonObject.get(key));
			}
		}
		JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[]{"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"}));
		sysUser = (SysUser) JSONObject.toBean(temp, SysUser.class);
	}
	
	/**
	 * 用户菜单
	 * @return
	 */
	public String treeMenu() {
		SysUser user = (SysUser) ServletActionContext.getRequest().getSession().getAttribute("SYS_USER");
		List<SysMenu> list = permissionService.userOwnMenu(user.getId());
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().print(TreeUtil.tree(list, false));
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}
	
	/**
	 * 登录
	 * @return
	 */
	public String login() {
		List<SysUser> list = sysUserService.getDataByCondition(sysUser);
		
		Integer flag = 0;
		if (list.size() > 0) {
			SysUser u = list.get(0);
			
			ServletActionContext.getRequest().getSession().setAttribute("SYS_USER", u);
			flag = 1;
		}
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().print(flag);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	
	/**
	 * 注销
	 * @return
	 */
	public String logout() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (null != session.getAttribute("SYS_USER")) {
			session.removeAttribute("SYS_USER");
		}
		
		return null;
	}
	
	/**
	 *	得到用户角色权限
	 */
	public String userRole() {
		if (null == ServletActionContext.getRequest().getParameter("id"))
			return null;
		
		Long id = Long.parseLong(ServletActionContext.getRequest().getParameter("id"));
		
		SysRole role = new SysRole();
		List<SysRole> list = sysRoleService.getDataByCondition(role);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		
		try {
			response.getWriter().print(TreeUtil.tree(list, true, permissionService.userOwnRole(id)));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 *	更新用户角色权限
	 */
	public String updateUserRole() {
		if (null == ServletActionContext.getRequest().getParameter("id"))
			return null;
		
		Long id = Long.parseLong(ServletActionContext.getRequest().getParameter("id"));
		
		String[] ids = ServletActionContext.getRequest().getParameter("ids").split(",");
		Long[] temp = new Long[ids.length];
		for (int i = 0; i < ids.length; i++) {
			if (ids[i].trim().equals(""))
				continue;
			
			temp[i] = Long.parseLong(ids[i]);
		}
		
		permissionService.updateUserRole(id, temp);
		
		return null;
	}
	
	
	/**
	 * 	得到角色菜单权限
	 */
	public String roleMenu() {
		if (null == ServletActionContext.getRequest().getParameter("id"))
			return null;
		
		Long id = Long.parseLong(ServletActionContext.getRequest().getParameter("id"));
		
		SysMenu menu = new SysMenu();
		List<SysMenu> list = sysMenuService.getDataByCondition(menu);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		
		try {
			response.getWriter().print(TreeUtil.tree(list, true, permissionService.roleOwnMenu(id)));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 *	更新角色菜单权限
	 */
	public String updateRoleMenu() {
		Long id = Long.parseLong(ServletActionContext.getRequest().getParameter("id"));
		
		String[] ids = ServletActionContext.getRequest().getParameter("ids").split(",");
		Long[] temp = new Long[ids.length];
		for (int i = 0; i < ids.length; i++) {
			if (ids[i].trim().equals(""))
				continue;
			
			temp[i] = Long.parseLong(ids[i]);
		}
		
		permissionService.updateRoleMenu(id, temp);
		
		return null;
	}
	
	/**
	 * 菜单在tree的位置
	 */
	public String menuLocation() {
		if (null == ServletActionContext.getRequest().getParameter("id"))
			return null;
		
		Long id = Long.parseLong(ServletActionContext.getRequest().getParameter("id"));
		
		SysMenu menu = new SysMenu();
		List<SysMenu> list = sysMenuService.getDataByCondition(menu);
		
		List<SysMenu> m = new ArrayList<SysMenu>();
		if (!id.equals(0L)) {
			m.add(sysMenuService.getDataById(id));
		}
		
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		
		try {
			response.getWriter().print(TreeUtil.tree(list, true, m, false));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	
}
